// pages/home/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        listInfo: [],
        imgUrls: [],
        messageTitle: '提示',
        message: '消息内容',
        showModalStatus: false,
        animationData: '',
        button: [
            { "classWrapper": "bd-bottom bd-right", "image": "icon-xuecar", "text": "学/练车", "bindTap": "clickxuecar" },
            { "classWrapper": "bd-bottom", "image": "icon-buycar", "text": "租/售/卖车", "bindTap": "clickbuycar" },
            { "classWrapper": "bd-right", "image": "icon-keepcark", "text": "售后保养", "bindTap": "clickkeepcark" },
            { "classWrapper": "", "image": "icon-newcar", "text": "社圈资讯", "bindTap": "clicknewcar" }
        ],
        upid:0 //扫码时获取上级id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        var that = this;
        var upuid = decodeURIComponent(e.scene);
        //console.log('scene=' + upuid);
        if (upuid != 'undefined' && parseInt(upuid)>0){
            wx.setStorageSync('upuid', upuid);
            app.globalData.upuid = upuid;
         }
        app.settingReadyCallback = (res) => { 
         // console.log(res);
          if (res.authSetting['scope.userInfo']) {
            app.userInfoReadyCallback = (res) => {
              if (res.errMsg == "getUserInfo:ok") {
                app.globalData.isLoginAuth = true;
                this.hideModal();
              }
              else {
                this.showModal();
                app.globalData.isLoginAuth = false;
              }
            }
          }
          else{
            if (!app.globalData.isLoginAuth) {
              this.showModal();
            }
          }
        }
       
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that=this;
        var listnav = app.globalData.listInfo;
        if (1 == wx.getStorageSync('roleidentity')) {
          var identity = wx.getStorageSync('roleFlag');
          wx.request({
            url: app.globalData.hostUrl + 'member/appointmentForsvrer',
            data: {
              identity: identity,
              uid: wx.getStorageSync('userId')
            },
            methods: 'GET',
            header: {
              "Content-Type": "json"
            },
            success: function (res) {
              if (res.data.result) {
                listnav[1].num = parseInt(res.data.result.lists.length) + parseInt(res.data.result.plxc.length);
                that.setData({
                  listInfo: listnav
                });
              }
            }
          })
        }
        else{
          this.setData({
            userinfo: wx.getStorageSync('session_userinfo'),
            listInfo: listnav
          })
        }
        this.setData({
            userinfo: wx.getStorageSync('session_userinfo'),
        })
        this.initBanner();
    },
    //广告图片初始化
    initBanner: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'common/banner';
        var imgbanner = wx.getStorageSync('indexBanner');
        //console.log(imgbanner.length);
        if (!app.expiratTime('indexBanner') || imgbanner.length == 0) {
            wx.request({
                url: requrl,
                data: { 'location': '101' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        //console.log(res);
                        wx.setStorageSync('indexBanner', res.data.result);
                        that.setData({
                            imgUrls: res.data.result
                        })
                    }
                }
            })
        }
        else {
            that.setData({
                imgUrls: imgbanner
            })
        }
    },
    clickxuecar:function(){
      wx.redirectTo({
            url: '../xuecar/index',
        })
    },
    clickkeepcark:function(){
      wx.redirectTo({
            url: '../keepcar/home',
        })
    },
    clicknewcar:function(){
      wx.redirectTo({
            url: '../news/index',
        })
    },
    clickbuycar:function(){
      wx.redirectTo({
        url: '../autocar/index',
      })
    },
  gohome: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  yuy: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/thappointment',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myappointment',
      })
    }
  },
  orders: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/trorder',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myorder',
      })
    }
  },
  mys: function () {
    wx.redirectTo({
      url: '../myself/myself',
    })
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.globalData.userInfo = e.detail.userInfo;
      app.getfansinfo();
    }
    else {
      wx.redirectTo({
        url: '../index/index',
      })
    }
  }
})